from sqlalchemy.orm import sessionmaker
from models.models import Teacher
from config.config import Config


class TeacherController:
    def __init__(self):
        self.engine = Config.engine
        self.Session = sessionmaker(bind=self.engine)

    def add_teacher(self, tno, tname, dno, salary, tmobile):
        session = self.Session()
        teacher = Teacher(Tno=tno, Tname=tname, Dno=dno, Salary=salary, Tmobile=tmobile)
        session.add(teacher)
        session.commit()
        session.close()

    def delete_teacher_by_tno(self, tno):
        session = self.Session()
        teacher = session.query(Teacher).filter_by(Tno=tno).first()
        if teacher:
            session.delete(teacher)
            session.commit()
        session.close()

    def query_teacher_by_name(self, name):
        session = self.Session()
        teachers = session.query(Teacher).filter(Teacher.Tname.like('%' + name + '%')).all()
        session.close()
        return teachers